import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Dashboard',
    component: () => import('../views/Dashboard.vue'),
    meta: { requiresAuth: false },
    children: [
      {
        path: '',
        redirect: 'home'
      },
      {
        path: 'home',
        name: 'Home',
        component: () => import('../views/Home.vue'),
        meta: { title: '首页', requiresAuth: false }
      },
      {
        path: 'department',
        name: 'Department',
        component: () => import('../views/department/Department.vue'),
        meta: { title: '部门管理', requiresAuth: true }
      },
      {
        path: 'position',
        name: 'Position',
        component: () => import('../views/position/Position.vue'),
        meta: { title: '岗位管理', requiresAuth: true }
      },
      {
        path: 'employee',
        name: 'Employee',
        component: () => import('../views/employee/Employee.vue'),
        meta: { title: '员工管理', requiresAuth: true }
      },
      {
        path: 'attendance',
        name: 'Attendance',
        component: () => import('../views/attendance/Attendance.vue'),
        meta: { title: '考勤管理', requiresAuth: true }
      },
      {
        path: 'leave',
        name: 'Leave',
        component: () => import('../views/leave/Leave.vue'),
        meta: { title: '请假管理', requiresAuth: true }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { title: '个人信息', requiresAuth: true }
      },
      {
        path: 'salary',
        name: 'Salary',
        component: () => import('../views/salary/Salary.vue'),
        meta: { title: '薪资管理', requiresAuth: true }
      },
      {
        path: 'payroll',
        name: 'Payroll',
        component: () => import('../views/employee/Payroll.vue'),
        meta: { title: '薪资发放', requiresAuth: true }
      },
      {
        path: 'ai',
        name: 'AI',
        component: () => import('../views/ai/Ai.vue'),
        meta: { title: '智能助手', requiresAuth: true }
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: () => import('../views/Notifications.vue'),
        meta: { title: '消息中心', requiresAuth: true }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const userInfo = localStorage.getItem('userInfo')

  // 1. 访问 /login 时，已登录直接跳转首页
  if (to.path === '/login') {
    if (token && userInfo) {
      try {
        const user = JSON.parse(userInfo)
        if (user.id && user.role) {
          next('/')
          return
        }
      } catch (error) {
        localStorage.clear()
      }
    }
  }

  // 2. 访问 / 时，无论是否登录都允许访问首页
  if (to.path === '/') {
    // 直接允许访问，不进行任何跳转
    next()
    return
  }

  // 3. 访问需要认证的页面，未登录跳转 login
  if (to.meta.requiresAuth) {
    if (!token || !userInfo) {
      next('/login')
      return
    }
    try {
      const user = JSON.parse(userInfo)
      if (!user.id || !user.role) {
        localStorage.clear()
        next('/login')
        return
      }
    } catch (error) {
      localStorage.clear()
      next('/login')
      return
    }
  }

  next()
})

export default router